package com.nstar.sinaweibo.model
{
	import flash.data.EncryptedLocalStore;
	import flash.utils.ByteArray;
	
	[Bindable]
	public class LoginData
	{
		public static const KEY_USER_NAME		:String = "username";
		public static const KEY_PASSWORD		:String = "password";
		public static const KEY_SAVE_PASS		:String = "savepass";
		public static const KEY_AUTO_LOGIN		:String = "autologin";
		public static const KEY_LOGIN_TIMES		:String = "loginTimes";
		public static const KEY_FOLLOWERS		:String = "followers";
		
		public var logined:Boolean = false;
		public var username:String = "";
		public var password:String = "";
		public var savePassword:Boolean = true;//是否保存密码
		public var autoLogin:Boolean = false;//是否自动登录
		public var loginTimes:int = 0;//登录次数
		public var followers:int = 0;
		
		//根据用户名称产生键名称，用以多个用户的数据存储
		//除用户名称和密码外，其它数据都以用户名称作为前缀
		private function getKey(key:String):String{
			return "["+username+"]"+key;
		}
		
		//保存用户名称和密码
		public function saveUserPassword():void{
			//保存用户信息到本地加密存储位置
			var userBytes:ByteArray = new ByteArray();
			userBytes.writeUTFBytes(username);

			var passBytes:ByteArray = new ByteArray();
			passBytes.writeUTFBytes(password);
			
			EncryptedLocalStore.setItem(KEY_USER_NAME,userBytes);//用户名称和密码不加前缀
			//是否保存密码
			if(savePassword){
				EncryptedLocalStore.setItem(KEY_PASSWORD,passBytes);
			}else{
				EncryptedLocalStore.setItem(KEY_PASSWORD,new ByteArray());
			}
		}
		
		//获取用户名称和密码
		public function loadUserPassword():void{
			//先把用户名称和密码读取出来，后面getKey()方法需要用到名称
			var userBytes:ByteArray = EncryptedLocalStore.getItem(KEY_USER_NAME);
			var passBytes:ByteArray = EncryptedLocalStore.getItem(KEY_PASSWORD);
			if(userBytes){
				username = userBytes.readUTFBytes(userBytes.length);
			}
			if(passBytes){
				password = passBytes.readUTFBytes(passBytes.length);
			}
		}
		
		//保存用户其它信息(除名称和密码)
		public function save():void{
			logined = true;
			//先保存用户名称和密码
			saveUserPassword();
			
			var passBytes:ByteArray = new ByteArray();
			passBytes.writeUTFBytes(password);
			
			var savePass:ByteArray = new ByteArray();
			savePass.writeUTFBytes(savePassword?"Y":"N");
			
			var autoLoginBytes:ByteArray = new ByteArray();
			//autoLoginBytes.writeUTFBytes(autoLogin?"Y":"N");
			autoLoginBytes.writeUTFBytes("N");
			
			var followersCount:ByteArray = new ByteArray();
			followersCount.writeInt(followers);

				
			EncryptedLocalStore.setItem(getKey(KEY_SAVE_PASS),savePass);	
			EncryptedLocalStore.setItem(getKey(KEY_AUTO_LOGIN),autoLoginBytes);	
			//保存登录次数
			var loginTimesBytes:ByteArray = EncryptedLocalStore.getItem(getKey(KEY_LOGIN_TIMES));
			if(loginTimesBytes){
				loginTimes = loginTimesBytes.readInt();
				loginTimes++;
				trace("loginTimes="+loginTimes);
				loginTimesBytes.clear();
				loginTimesBytes.writeInt(loginTimes);
				EncryptedLocalStore.setItem(getKey(KEY_LOGIN_TIMES),loginTimesBytes);	
			}else{
				trace("first login.");
				loginTimes = 1;
				loginTimesBytes = new ByteArray();
				loginTimesBytes.writeInt(loginTimes);
				EncryptedLocalStore.setItem(getKey(KEY_LOGIN_TIMES),loginTimesBytes);
				
			}
			
			//粉丝数
			EncryptedLocalStore.setItem(getKey(KEY_FOLLOWERS),followersCount);
			
			trace("save user info ok. loginTimes="+loginTimes);
		}
		
		//保存粉丝数
		public function saveFollowers(followersCount:int):void{
			followers = followersCount;
			var followersByte:ByteArray = new ByteArray();
			followersByte.writeInt(followersCount);
			EncryptedLocalStore.setItem(getKey(KEY_FOLLOWERS),followersByte);
		}
		
		//获取本地加密的用户信息
		public function load():void{
			//其它信息
			var savePassBytes:ByteArray = EncryptedLocalStore.getItem(getKey(KEY_SAVE_PASS));
			var autoLoginBytes:ByteArray = EncryptedLocalStore.getItem(getKey(KEY_AUTO_LOGIN));
			var loginTimesBytes:ByteArray = EncryptedLocalStore.getItem(getKey(KEY_LOGIN_TIMES));
			var followersBytes:ByteArray = EncryptedLocalStore.getItem(getKey(KEY_FOLLOWERS));
			
			if(savePassBytes){
				var s:String = savePassBytes.readUTFBytes(savePassBytes.length);
				savePassword = (s=="Y");
			}
			
			if(autoLoginBytes){
				var s:String = autoLoginBytes.readUTFBytes(autoLoginBytes.length);
				autoLogin = (s=="Y");
			}
			
			if(loginTimesBytes){
				loginTimes = loginTimesBytes.readInt();
			}
			
			if(followersBytes){
				followers = followersBytes.readInt();
			}
			
			trace("load user info ok.\n"+toString());
		}
		
		public function reset():void{
			EncryptedLocalStore.reset();
		}
		
		public function clearItem(key:String):void{
			EncryptedLocalStore.removeItem(key);
		}
		
		
		public function toString():String{
			return "logined = "+logined+"\n"+
				   "username = "+username+"\n"+
				   "password = **********\n"+
				   "savePassword = "+savePassword+"\n"+
				   "autoLogin = "+autoLogin+"\n"+
				   "loginTimes = "+loginTimes+"\n"+
				   "followers = "+followers+"\n";
		}
	}
}